using Godot;
using Godot.Collections;
using System;

public partial class GuiViewManager : CanvasLayer
{
	[Export]
	public Array<GuiViewConfig> viewConfigsList{get;set;}//界面列表
	[Export]
	public Node rootGui;
	private Map _map;
	private Player _player;
	public Camera2D _camera;
	public Dictionary<string,GuiViewConfig> viewConfigMap = new Dictionary<string,GuiViewConfig>();
	public int viewInstanceCount = 0;//实例化界面的数量
	public Dictionary<string,Ui> viewInstanceMap = new Dictionary<string,Ui>();
	public Array<Ui> InstanceUIsList = new Array<Ui>();
	public override void _Ready()
	{
		Node root = this.GetChild(0);
		build_view_config_map();
		var children = root.GetChildren();
		foreach (var child in children)
		{
			if(child.Name == "GUIRoot")
			{continue;}
			Ui UI = (Ui)child;
			UI.Initialize();
		}
		var Game = GetNode<Game>("/root/Game");
		_map = Game.get_map();
		_player = _map.GetNode<Player>("%Player");
		_camera = _player.GetNode<Camera2D>("Camera2D");
		this.FollowViewportEnabled = true;
	}

    public override void _Process(double delta)
    {
        base._Process(delta);
		var Game = GetNode<Game>("/root/Game");
		if(Input.IsActionJustReleased("esc")&&Game._timer.IsStopped()&&InstanceUIsList.Count != 0)
		{
			//GD.Print("anesc");
			//GD.Print(InstanceUIsList.Count);
			//GD.Print(viewInstanceCount);
			InstanceUIsList[InstanceUIsList.Count-1].close_self();
			//GD.Print(InstanceUIsList.Count);
			//GD.Print(viewInstanceCount);
		}
    }

    public void build_view_config_map()
	{
		Node root = this.GetChild(0);
		foreach (var config in viewConfigsList)
		{
			if(config ==null || config.id == string.Empty)
			{
				continue;
			}
			viewConfigMap[config.id] = config;
			var path = (string)config.id;
			var v = root.GetNode<Ui>((NodePath)path);
			viewInstanceMap[config.id] = (Ui)v;
			v.viewInstanceID = config.id;
		}
		
	}

	public GuiViewConfig get_view_config(StringName viewID)//获取界面
	{
		return viewConfigMap[viewID];
	}

	public int new_view_instance_ID()
	{
		var t = viewInstanceCount++;
		return t;
	}

	public Ui get_view_instance(string ID)
	{
		return viewInstanceMap[ID];
	}

	public void open_view(StringName viewID)
	{
		//var config = get_view_config(viewID);
		//var instanceID = new_view_instance_ID();
		//PackedScene prefab = config.prefab;
		CanvasLayer root = this.GetChild<CanvasLayer>(0);
		Camera2D camera = _player.GetNode<Camera2D>("Camera2D");
		//root.Offset = _player.Position - camera.Offset + new Vector2(-1920/8,-1080/8);
		//var v = prefab.Instantiate() as Ui;
		var path = (string)viewID;
		var v = root.GetNode<Ui>((NodePath)path);
		//v.config = config;
		//v.viewInstanceID = instanceID;
		//viewInstanceMap[path] = v;
		//rootGui.AddChild(v);
		v.Visible = true;
		//v.Position = _player.Position + _camera.Position -v.GetChild<Control>(0).Size/2;
		v.enter();
		//return instanceID;
	}

	public void close_view(string viewinstanceID)
	{
		Node root = this.GetChild(0);
		var path = (string)viewinstanceID;
		var v = root.GetNode<Ui>((NodePath)path);
		v.Visible = false;
		v.exit();
		
		//GD.Print(viewInstanceCount);
		
		
	}
	public void ShowTips(string tips)
	{
		this.GetNode<UiTips>("%UiTips").ShowTips(tips);
	}
}
